package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.article.mapper.TravelMapper;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper contentMapper;

    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo(list);
    }

    public TravelContent getContent(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    public void updateState(Integer state, Long id) {
        travelMapper.updateState(state, id);
    }

    public Travel get(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    public List<Travel> listViewnumTop3(Long destId) {
        return travelMapper.listViewnumTop3(destId);
    }

    public AjaxResult saveOrUpdate(Travel travel) {
        AjaxResult result = new AjaxResult();
        //游记内容
        TravelContent content = travel.getTravelContent();


        travel.setSummary("ai生成");
        Date date = new Date();
        travel.setLastUpdateTime(date);
        if (travel.getId() == null) {
            travel.setCreateTime(date);
            travel.setReleaseTime(date);

            travel.setState(Travel.STATE_RELEASE);//模拟直接审核通过

            travelMapper.insert(travel);
            //保存游记内容
            content.setId(travel.getId());
            contentMapper.insert(content);

        } else {
            travelMapper.updateByPrimaryKey(travel);
            //修改游记内容
            content.setId(travel.getId());
            contentMapper.updateByPrimaryKey(content);
        }

        return result.addData(travel.getId());  //返回当前对象 的id.

    }

    public List<Travel> list() {
        return travelMapper.selectAll();
    }


}
